<template>
	<yzb-page class="">
		<view class="top">
			<view class="search">
				<view class="search-input">
					<text class="yzb yzb-search"></text>
					<input class="text-normal" type="text" :value="searchValue" :placeholder="placeholder" @input="searchInput" />
				</view>
				<view class="btn-cancel" @click="cancelSearch">清空</view>
			</view>
			<template v-if="!query.type">
				<yzbfilterDropdown
					:menuTop="0"
					:filterData="filterData"
					:defaultSelected="defaultSelected"
					:updateMenuName="true"
					@confirm="confirm"
					dataFormat="Object"
				></yzbfilterDropdown>
			</template>
		</view>
		<view :class="query.type?'body-list2':'body-list'">
			<m-company :list="list" @click="detail">
			</m-company>
			<view class="load-more-box">
				<uni-load-more v-if="status == '请求中'" status="正在加载..." :showIcon="true"></uni-load-more>
				<uni-load-more v-if="status == '没有更多'" status="没有更多了" :showIcon="false"></uni-load-more>
				<uni-load-more v-if="status == '暂无数据'" status="暂无数据" :showIcon="false"></uni-load-more>
				<uni-load-more v-if="status == '请求失败'" status="加载失败，点我重试" :showIcon="false" @click="reLoad"></uni-load-more>
			</view>
		</view>
	</yzb-page>
</template>

<script>
import { mapState, mapGetters } from 'vuex';
import mCompany from '@/components/m-company/m-company.vue';
import yzbfilterDropdown from '@/components/yzb/yzb-filterDropdown.vue';
export default {
	components: {
		mCompany,
		yzbfilterDropdown
	}, 
	computed: {
		...mapState(['forcedLogin']),
		...mapGetters(['hasLogin'])
	},
	data() {
		return {
			list: [],
			page: 1,
			limit: 10,
			status:"",
			query: {
				page: 1,
				limit: 10,
				keyword: '',
				industryId: '',
				companyNature: '',
				staffSize: '',
				type: '',
				verifyStatus:3
			},
			
			searchValue: '',
			placeholder: '请输入关键词搜索相关内容',
			defaultSelected:[],
			filterData: [
				{
					name: '行业筛选',
					type: 'hierarchy',
					child: []
				},
				{
					name: '公司性质',
					type: 'hierarchy', //filter
					child: []
				},
				{
					name: '人员规模',
					type: 'hierarchy',
					child: []
				},
				// {
				// 	name: '公司福利',
				// 	type: 'filter',
				// 	child: []
				// },
			]
		};
	},
	async onLoad(query) {
		if (query.text) {
			uni.setNavigationBarTitle({
				title: query.text
			});
		}
		if(query.type){
			this.query.type=query.type;
		}
		this.getList();
		this.initData();
	},
	
	onReachBottom() {
		this.query.page++;
		this.getList();
	},
	
	methods: {
		
		async initData() {
			let all = {
				name: '不限',
				id: '不限'
			};
			let industryList = await this.$apis.getIndustryList();
			industryList.unshift(all);
			let natureList = await this.$apis.getDictItems({dictCode: 'company_nature'});
			natureList.unshift(all);
			let staffSizeList = await this.$apis.getDictItems({dictCode: 'staff_size'});
			staffSizeList.unshift(all);
			this.filterData[0].child = industryList;
			this.filterData[1].child = natureList;
			this.filterData[2].child = staffSizeList;
			console.log('industryList', industryList);
			console.log('natureList', natureList);
			console.log('staffSizeList', staffSizeList);
		},
		
		searchInput(e) {
			this.searchValue = e.detail.value;
			console.log('searchInput', this.searchValue);
			if (this.searchValue) {
				this.query.page = 1;
				this.query.keyword = this.searchValue;
				this.getList();
			} else {
				this.cancelSearch();
			}
		},
		// 取消搜索
		cancelSearch() {
			this.searchValue = '';
			this.query.keyword = '';
			this.query.page = 1;
			this.getList();
		},
		
		confirm(options) {
			console.log('--confirm--', options);
			if (options.value[0][1] != null) {
				this.query.industryId = options.value[0][1];
			} else {
				this.query.industryId = '';
			}
			if (options.value[1][0] != null && options.value[1][0] != '不限') {
				this.query.companyNature = options.value[1][0];
			} else {
				this.query.companyNature = '';
			}
			if (options.value[2][0] != null && options.value[2][0] != '不限') {
				this.query.staffSize = options.value[2][0];
			} else {
				this.query.staffSize = '';
			}
			this.query.page=1;
			this.getList()
		},
		
		async getList() {
			this.query.latitude = getApp().globalData.location.latitude;
			this.query.longitude = getApp().globalData.location.longitude;
			this.query.pcitycode = getApp().globalData.location.pcitycode;
			this.status = '请求中';
			let res = await this.$apis.getCompanyList(this.query);
			if (res) {
				let data = res.records;
				if(this.query.page==1){
					this.list=[];
				}
				this.list = this.list.concat(data || []);
				this.changeStatus(res);
			}
			uni.stopPullDownRefresh();
		},
		
		// 修改请求状态
		changeStatus(data) {
			if (this.list.length === 0) {
				this.status = '暂无数据';
			} else if (this.query.page >= Math.ceil(data.total / this.query.limit)) {
				this.status = '没有更多';
			} else {
				this.status = '请求更多';
			}
		},
		
		detail(item) {
			this.$mRouter.push({
				route: this.$mRoutesConfig.companyDetail,
				query: {
					id: item.id
				}
			});
		},
	}
};
</script>

<style lang="scss" scoped>
.top {
	width: 100%;
	// height: 180upx;
	position: fixed;
	/* #ifdef H5 */
	top: 44px;
	/* #endif */
	/* #ifndef H5 */
	top: 0;
	/* #endif */
	z-index: 99;
	display: flex;
	flex-direction: column;
}

.search {
	width: 100%;
	display: flex;
	height: 90upx;
	z-index: 999;
	flex-direction: row;
	background-color: $main-color;
	color: #ffffff;
	align-items: center;
	justify-content: center;
	.yzb-search {
		color: $font-color-999;
		font-size: $uni-font-size-base;
		margin: 0 15upx;
		margin-top: 8upx;
	}
	.search-input {
		width: 77%;
		height: 65upx;
		border-radius: 50upx;
		display: flex;
		flex-direction: row;
		background-color: #ffffff;
		align-items: center;
	}
	.search-input image {
		width: 35upx;
		height: 35upx;
		margin-right: 10upx;
	}
	.search-input input {
		width: 65%;
		font-size: 28upx;
		color: $font-color-333;
	}

	.btn-search {
		margin-left: 20upx;
	}

	.btn-cancel {
		margin-left: 20upx;
	}
}

.body-list2{
	position: relative;
	margin-top: 100upx;
}

.body-list{
	position: relative;
	margin-top: 170upx;
}

.notice {
	width: 100%;
	// padding: 3%;
	margin-top: 20upx;
	padding: 20upx 0;
	background-color: #ffffff;
	.content {
		width: 90%;
		padding: 20upx 15upx;
		margin: 0 auto;
		border-radius: $uni-border-radius-lg;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		.notice-icon {
			color: $main-color;
			font-size: $font-size-36;
		}
		.notice-text {
			color: $font-color-light;
			text {
				color: $font-color-dark;
				font-weight: bold;
				padding: 0 15upx;
			}
		}
		.notice-next {
			font-size: $font-size-34;
			color: $font-color-light;
		}
	}
}

.post {
	margin-top: 2upx;
	background-color: #ffffff;
	padding: 20upx;
	.title {
		font-weight: bold;
		font-size: $uni-font-size-lg;
	}

	.post-list {
		overflow: hidden;
		white-space: nowrap;
		flex-wrap: wrap;
		width: 96%;
		padding: 2%;
	}
	.post-item {
		// background-color: #F7F7F7;
		border: 1upx solid $border-color-base;
		font-size: $uni-font-size-sm;
		float: left;
		width: 22%;
		padding: 15upx 0upx;
		border-radius: 8upx;
		margin-top: 20upx;
		text-align: center;
	}
	.post-item-left {
		margin-left: 3%;
	}

	.all {
		margin-top: 10upx;
		display: flex;
		justify-content: center;
		align-items: center;
		.all-text {
			color: $main-color;
			font-weight: bold;
		}
		.post-next {
			font-size: $font-size-34;
			color: $main-color;
			margin-left: 5upx;
		}
	}
}

.expect {
	padding: 25upx 2%;
	margin-top: 20upx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	flex-direction: row;
	background-color: $bgcolor_white;
	border-bottom: 1upx solid $border-color-base;
	.items {
		width: 80%;
		padding: 0 3%;
		flex-wrap: wrap;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		white-space: nowrap;
		align-items: center;
		text {
			padding: 0 10upx;
			font-size: $uni-font-size-lg;
			color: $font-color-base;
		}
		.selected {
			font-weight: bold;
			color: $font-color-000;
			font-size: $font-size-34;
		}
	}
	.expect-icon {
		size: $uni-font-size-lg;
		color: $font-color-gray;
		margin-right: 10upx;
	}
}
</style>
